package com.jp.knowledge.util;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.internal.LinkedTreeMap;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiayen on 16/9/28.
 */
public class JsonUtil {

    private static JsonUtil instance;
    private Gson gson;

    private JsonUtil() {
        gson = new Gson();
    }

    public static JsonUtil getInstance() {
        if (instance == null) instance = new JsonUtil();

        return instance;
    }

    public static Object getValue(String key, JsonObject object) {
        try {
            return object.get(key).getAsJsonObject();
        } catch (Exception e) {
            e.printStackTrace();
            L.e(e.getMessage());
        }
        return "";
    }

    public JsonObject strToJson(String str) {
        try {
            JsonObject obj = new JsonParser().parse(str).getAsJsonObject();
            return obj;
        }catch (Exception e){
                L.e(e.getMessage());
        }
      return null;
    }

    public String objToJson(Object obj){
        return gson.toJson(obj);
    }

    public JsonObject   entityToJson(Object obj){
        try{
            return gson.toJsonTree(obj).getAsJsonObject();
        }catch (Exception e){

        }
       return  null;
    }

    /**
     * json  to  实体
     *
     * @param object
     * @param clazz
     * @param <T>
     * @return
     */
    public <T extends Object> T jsonToObject(JsonObject object, Class<T> clazz) {
        return gson.fromJson(object, clazz);
    }

    /**
     * jsonstr  to  实体
     *
     * @param str
     * @param clazz
     * @param <T>
     * @return
     */
    public <T extends Object> T strToObject(String str, Class<T> clazz) {
        return gson.fromJson(str, clazz);
    }

    /**
     * 字符串转实体
     *
     * @param str
     * @param clazz
     * @param <T>
     * @return
     */
    public <T extends Object> T jsonToObject(String str, Class<T> clazz) {
        return gson.fromJson(str, clazz);
    }

    /**
     * 数组转实体
     *
     * @param array
     * @param clazz
     * @param <T>
     * @return
     */
    public <T extends Object> List<T> jsonArrayToObject(JsonArray array, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        int size = array.size();
        JsonObject obj = null;
        for (int i = 0; i < size; i++) {
            try {
                obj = array.get(i).getAsJsonObject();
                list.add(jsonToObject(obj, clazz));
            } catch (Exception e) {
                L.e(e.getMessage());
            }
        }
        return list;
    }


    /**
     * 字符串 转list
     *
     * @param arrayStr
     * @param clazz
     * @param <T>
     * @return
     */
    public <T extends Object> List<T> jsonArrayToObject(String arrayStr, Class<T> clazz) {
        JsonArray array = new JsonParser().parse(arrayStr).getAsJsonArray();
        List<T> list = jsonArrayToObject(array, clazz);
        return list;
    }

    /**
     * 字符串 转list
     *
     * @param arrayStr
     * @param clazz
     * @param <T>
     * @return
     */
    public <T extends Object> T mapToObject(LinkedTreeMap<String, Object> arrayStr, Class<T> clazz) {
//        JsonArray array = new JsonParser().parse(arrayStr);
        try {
            String json = gson.toJson(arrayStr);
            T t = gson.fromJson(json, clazz);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
            L.e(e.getMessage());
        }
        return null;
    }

    /**
     * 字符串 转list
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T extends Object> List<T> listMapToObject(ArrayList<LinkedTreeMap<String, Object>> array, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        try {
            for (LinkedTreeMap<String, Object> map : array) {
                list.add(mapToObject(map, clazz));
            }
        } catch (Exception e) {
            e.printStackTrace();
            L.e(e.getMessage());
        }
        return list;
    }

}
